package com.example.jiaoan;

import androidx.annotation.NonNull;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import org.json.JSONException;
import org.json.JSONObject;

public class MainActivity extends BaseActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContext(this);
        setContentView(R.layout.activity_main);

        ImageView icon = findViewById(R.id.web_icon);
        TextView title = findViewById(R.id.web_title);

        Button button = findViewById(R.id.loginBtn);
        button.setOnClickListener(this);

        getConfig(new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(@NonNull Message message) {
                if (message.what == ERROR_STATE || message.what == FAIL_STATE) {
                    // 处理网络请求失败的情况
                } else if (message.what == SUCCESS_STATE) {
                    JSONObject resp = (JSONObject) message.obj;
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                setWebImage(resp.getString("logo"), icon);
                                title.setText(resp.getString("name"));
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    });
                }
                return false;
            }
        }));
    }

    @Override
    public void onClick(View v) {
        EditText username = findViewById(R.id.username);
        EditText password = findViewById(R.id.password);

        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("username",username.getText());
            jsonObject.put("password",password.getText());

            String jsonStr = jsonObject.toString();

            commonRequest("https://teach.vshare.site/adminapi/index/login",new Handler(new Handler.Callback() {
                @Override
                public boolean handleMessage(@NonNull Message msg) {
                    if (msg.what == ERROR_STATE || msg.what == FAIL_STATE) {
                        // 处理网络请求失败的情况

                    } else if (msg.what == SUCCESS_STATE) {
                        JSONObject resp = (JSONObject) msg.obj;
                        // 存储登录信息
                        try {
                            saveLoginData(resp.getString("token"),resp.getString("avatar"),username.getText().toString());
                            toView(MainActivity.this,HomeActivity.class,false,0);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                    return false;
                }
            }),false,null,true,jsonStr);
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }
}